﻿Public Class perNcm
    Inherits perAcessoBD

    Public Sub excluirNCM(ByVal sNCM As String)
        Dim sSql As String

        sSql = "Delete NCM"

        If sNCM <> String.Empty Then
            sSql &= " WHERE NCM = @Ncm "
        End If

        With MyBase.SQLCmd.Parameters
            .Clear()
            If sNCM <> String.Empty Then
                .AddWithValue("@Ncm", sNCM)
            End If
        End With

        MyBase.Executar_Comando(eTipoComando.Acao, sSql)

    End Sub

    Public Function selecionarNCM(ByVal sNCM As String) As DataTable
        Dim sSql As String
        Dim dtRetorno As New DataTable

        sSql = "Select * From NCM"

        If sNCM <> String.Empty Then
            sSql &= " Where NCM = @ncm "
        End If

        With MyBase.SQLCmd.Parameters
            .Clear()
            .AddWithValue("@ncm", sNCM)
        End With

        dtRetorno = MyBase.executarConsulta(sSql)

        If dtRetorno.Rows.Count = 0 Then
            Return Nothing
        Else
            Return dtRetorno
        End If

    End Function

    Public Sub inserirNCM(ByVal sNCM As String, ByVal dIPI As Double)
        Dim sSql As String

        sSql = "Insert Into NCM(" &
               "                NCM " &
               "                ,IPI" &
               "               )" &
               "       Values ( " &
               "               @NCM" &
               "               ,@IPI" &
               "              ) "

        With MyBase.SQLCmd.Parameters
            .Clear()
            .AddWithValue("@NCM", sNCM)
            .AddWithValue("@IPI", dIPI)
        End With

        MyBase.executarAcao(sSql)

    End Sub

    Public Function atualizarNCMs() As DataTable
        Dim sSql As String = ""
        Dim datRetorno As New DataTable

        sSql = "Select distinct(ncm) " &
               "  From Produto " &
               "  Where Ltrim(Rtrim(ncm)) Not In (Select Ltrim(Rtrim(ncm)) From ncm) " &
               "    And ncm <> '' "
        With MyBase.SQLCmd.Parameters
            .Clear()
        End With
        datRetorno = MyBase.executarConsulta(sSql)

        If datRetorno.Rows.Count = 0 Then
            Return Nothing
        Else
            Return datRetorno
        End If


    End Function


End Class
